package com.sansan.ultrasound_handpad.base;

import android.annotation.SuppressLint;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.sansan.ultrasound_handpad.R;

public class SoundActivity extends BaseActivity implements View.OnClickListener {

    private Button btnPlay;
    private Button btnPlay2;

    private SoundPool soundPool;//声明一个SoundPool
    private int soundID;//创建某个声音对应的音频ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        btnPlay = (Button) findViewById(R.id.btnPlay);
        btnPlay.setOnClickListener(this);

        initSound();
        System.out.println(btnPlay.getText());

        /*btnPlay2 = (Button) findViewById(R.id.btnPlay2);
        btnPlay2.setOnClickListener(this);
        initSound2();
        System.out.println(btnPlay2.getText());*/




    }

    @SuppressLint("NewApi")
    private void initSound() {
        soundPool = new SoundPool.Builder().build();
        soundID = soundPool.load(this, R.raw.shangliao, 1);
    }
    /*@SuppressLint("NewApi")
    private void initSound2() {
        soundPool = new SoundPool.Builder().build();
        soundID = soundPool.load(this, R.raw.finish, 1);
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnPlay:
            /*case R.id.btnPlay2:*/
                playSound();
                break;
        }
    }

    private void playSound() {
        soundPool.play(
                soundID,
                0.1f,      //左耳道音量【0~1】
                0.5f,      //右耳道音量【0~1】
                0,         //播放优先级【0表示最低优先级】
                0,         //循环模式【0表示循环一次，-1表示一直循环，其他表示数字+1表示当前数字对应的循环次数】
                1          //播放速度【1是正常，范围从0~2】
        );
    }
}